import * as Router from 'koa-router';
import { Routers } from '../data/routers';
import apis from '../apis';

const router = new Router();

/**
 * Base route, return a 401
 */
router.get('/', async ctx => ctx.status = 401);

/**
 * Basic healthcheck
 */
router.get('/healthcheck', async ctx => ctx.body = 'OK');

export default async () => {
  const localRoutes = await apis();
  // console.log('apis=', apis);
  // const keys = Object.keys(localRoutes);
  Routers.list = localRoutes; // 保存全部路由信息，鉴权判断使用
  localRoutes.forEach(r => {
    router[r.module.method](r.route, async ctx => {
      const response = await r.module.request(ctx);
      ctx.body = response;
    });
  });
  // console.log('localRoutes=', localRoutes);
  return {
    routers: localRoutes,
    call: router.routes()
  };
};


// export const routes = router.routes();
